package com.atguigu.flinkSideOutput;


import com.atguigu.been.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

//侧输出流初体验

public class Window_EventTime_SideOutput_Bounded_WaterMark {
    public static void main(String[] args) throws Exception {
        //1.流的执行环境
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        senv.setParallelism(1);

        //2.获取数据
        DataStreamSource<String> streamSource = senv.socketTextStream("hadoop102", 9999);

        //3.对数据进行处理，封装成WaterSensor
        SingleOutputStreamOperator<WaterSensor> waterSensorDStream = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });
        //todo 指定waterMarks 和最大异步时间 所以窗口的执行时间为
        // 窗口时间 + 延迟时间 (5+2) = 7 即 时间戳为7的数据进来时才会处理[0,5)窗口的数据
        SingleOutputStreamOperator<WaterSensor> watermarks = waterSensorDStream.assignTimestampsAndWatermarks(
                WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(2)) // 最大容忍的延迟时间
                        .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() { //指定时间戳
                            @Override
                            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                return element.getTs() * 1000; //事件时间戳 即模拟时间发生时间
                            }
                        })
        );

        KeyedStream<WaterSensor, String> keyedStream = watermarks.keyBy(WaterSensor::getId);
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream.
                window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(2)); //窗口时间
        //todo开启侧输出流
        window.sideOutputLateData(new OutputTag<WaterSensor>("side_1") {
        });

        SingleOutputStreamOperator<String> streamOperator = window.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            @Override
            public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                String msg = "当前key: " + s
                        + "窗口: [" + context.window().getStart() / 1000 + "," + context.window().getEnd() / 1000 + ") 一共有 "
                        + elements.spliterator().estimateSize() + "条数据 ";
                out.collect(context.window().toString());
                out.collect(msg);

            }
        }); //这些东西只有主流打印侧流不打印
        streamOperator.print("主流:");
        streamOperator.getSideOutput(new OutputTag<WaterSensor>("side_1") {
        }).print("侧输出流:");
        senv.execute();
    }
}
